﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace YJKView
{
	public class ExtensibleStorageManager
	{
		public static void WriteRampExtensibleStorage(Element elem, double angle)
		{
			try
			{
				Guid guid = new Guid(ExtensibleStorageManager.m_GUID);
				Schema schema = Schema.Lookup(guid);
				if (schema == null)
				{
					SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
					schemaBuilder.SetSchemaName("HY_View_ESData");
					FieldBuilder fieldBuilder = schemaBuilder.AddSimpleField("Angle", typeof(double));
					fieldBuilder.SetUnitType(0);
					schema = schemaBuilder.Finish();
				}
				Entity entity = new Entity(schema);
				Field field = schema.GetField("Angle");
                entity.Set<double>(field, angle, (DisplayUnitType)6);
				elem.SetEntity(entity);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示");
			}
		}

		public static bool ReadRampExtensibleStorage(Element elem, ref double angle)
		{
			bool result;
			try
			{
				Guid guid = new Guid(ExtensibleStorageManager.m_GUID);
				Entity entity = elem.GetEntity(Schema.Lookup(guid));
				if (entity == null)
				{
					result = false;
				}
				else
				{
                    angle = entity.Get<double>(Schema.Lookup(guid).GetField("Angle"), (DisplayUnitType)6);
					result = true;
				}
			}
			catch
			{
				result = false;
			}
			return result;
		}

		private static string m_GUID = "D7EA64A1-BA1E-4454-B16C-8C1C9CB7A5B3";
	}
}
